package com.kdgz.kdgz.thread;

import com.kdgz.kdgz.beans.Orders;
import com.kdgz.kdgz.beans.Rule;
import com.kdgz.kdgz.beans.Salarys;
import com.kdgz.kdgz.beans.Staff;
import com.kdgz.kdgz.sevice.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author Veneno
 * @Desc 模拟每月初统计上一月收益
 * @date 2021/3/20
 */
@Component
@Order(value = 12)
@SpringBootTest
public class FinanceThread implements ApplicationRunner {
    @Autowired
    public FinanceService financeService;
    @Autowired
    public StaffService staffService;
    @Autowired
    public OrdersService ordersService;
    @Autowired
    public RuleService ruleService;
    @Autowired
    public SalaryService salaryService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    Calendar date = Calendar.getInstance();
                    if (date.get(Calendar.DATE) == 1) {
                        financeService.insertData(date.get(Calendar.YEAR),date.get(Calendar.MONTH));
                        addSalarys();
                    }
                    try {
                        Thread.sleep(1000*60*60*24);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        new Thread(runnable).start();
    }

    @Test
    public void addSalarys() {
        Rule rule = ruleService.selectRule();
        List<Staff> list = staffService.list();
        for (Staff staff : list) {
            BigDecimal salary = staff.getBaseInfo().getSalary();
            if (staff.getBid()==999) {
                continue;
            }else if (staff.getBid()/100 == 1){
                // 该快递员本月所有快递信息
                List<Orders> ordersBySidInMonth = ordersService.getOrdersBySidInMonth(staff.getSid(), new Date().getYear(), new Date().getMonth());
                BigDecimal money = new BigDecimal(0);
                for (Orders orders : ordersBySidInMonth) {
                    money = money.add(orders.getPrice());
                }
                // 总工资 = 快递数量*单价提成+快递总运费*百分比提成+基本工资
                BigDecimal sum =  new BigDecimal(ordersBySidInMonth.size()*rule.getOneFee()).add(money.multiply(new BigDecimal(rule.getPerFee())).add(salary));
                // 插入数据库（员工id、工资、年月）
                salaryService.insertSalary(new Salarys(staff.getSid(), sum, new Date()));
            }else{
                // 插入数据库（员工id、工资、年月）
                 salaryService.insertSalary(new Salarys(staff.getSid(), salary, new Date()));
            }
        }
    }
}
